{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "file_name = r\"C:\\迅雷下载\\utf-8''accc7cce-ba3d-11ec-9bce-00163e012765_20220412165859\\20220412105554283_14559.csv\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "              cargo_id           shipper_id cargo_name  first_category_id  \\\n0       32413588863147  3988213644352553147         化肥                 13   \n1       32414135827834    96436391904597834         椰子              10000   \n2       32414137990792  4000000000002670792        胡萝卜              10001   \n3       32414138875050  4000000000008475050         色粉                 14   \n4       32414140065568  4000000015543125568       食品饮料                 10   \n...                ...                  ...        ...                ...   \n898841  32438590365953    95819431532065953        包装盒                  7   \n898842  32438591265832    96436391031185832       环保设备                  4   \n898843  32438591885291  3988216899619015291         煤炭                  6   \n898844  32438592920699    96436391873310699         煤块                  6   \n898845  32438593022063    96436391207002063  玉米作物（干玉米）              10002   \n\n        second_category_id                                          nlpresult  \n0                      156  {\"cargoName\":\"化肥\",\"cargoText\":\"化肥\",\"truckTypeN...  \n1                    10011  {\"cargoName\":\"椰子\",\"cargoText\":\"椰子\",\"truckTypeN...  \n2                    10014  {\"cargoName\":\"胡萝卜\",\"cargoText\":\"胡萝卜\",\"truckTyp...  \n3                      106  {\"cargoName\":\"色粉\",\"cargoText\":\"色粉\",\"truckTypeN...  \n4                      200  {\"cargoName\":\"食品饮料\",\"cargoText\":\"食品饮料\",\"truckT...  \n...                    ...                                                ...  \n898841                  77  {\"cargoName\":\"包装盒\",\"cargoText\":\"包装盒\",\"truckTyp...  \n898842                  60  {\"cargoName\":\"环保设备\",\"cargoText\":\"环保设备\",\"truckT...  \n898843                  73  {\"cargoName\":\"煤炭\",\"cargoText\":\"煤炭\",\"truckTypeN...  \n898844                  73  {\"cargoName\":\"煤块\",\"cargoText\":\"煤块\",\"truckTypeN...  \n898845               10024  {\"cargoName\":\"作物干玉米\",\"cargoText\":\"玉米作物（干玉米）\",\"...  \n\n[898846 rows x 6 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>cargo_id</th>\n      <th>shipper_id</th>\n      <th>cargo_name</th>\n      <th>first_category_id</th>\n      <th>second_category_id</th>\n      <th>nlpresult</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>32413588863147</td>\n      <td>3988213644352553147</td>\n      <td>化肥</td>\n      <td>13</td>\n      <td>156</td>\n      <td>{\"cargoName\":\"化肥\",\"cargoText\":\"化肥\",\"truckTypeN...</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>32414135827834</td>\n      <td>96436391904597834</td>\n      <td>椰子</td>\n      <td>10000</td>\n      <td>10011</td>\n      <td>{\"cargoName\":\"椰子\",\"cargoText\":\"椰子\",\"truckTypeN...</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>32414137990792</td>\n      <td>4000000000002670792</td>\n      <td>胡萝卜</td>\n      <td>10001</td>\n      <td>10014</td>\n      <td>{\"cargoName\":\"胡萝卜\",\"cargoText\":\"胡萝卜\",\"truckTyp...</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>32414138875050</td>\n      <td>4000000000008475050</td>\n      <td>色粉</td>\n      <td>14</td>\n      <td>106</td>\n      <td>{\"cargoName\":\"色粉\",\"cargoText\":\"色粉\",\"truckTypeN...</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>32414140065568</td>\n      <td>4000000015543125568</td>\n      <td>食品饮料</td>\n      <td>10</td>\n      <td>200</td>\n      <td>{\"cargoName\":\"食品饮料\",\"cargoText\":\"食品饮料\",\"truckT...</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>898841</th>\n      <td>32438590365953</td>\n      <td>95819431532065953</td>\n      <td>包装盒</td>\n      <td>7</td>\n      <td>77</td>\n      <td>{\"cargoName\":\"包装盒\",\"cargoText\":\"包装盒\",\"truckTyp...</td>\n    </tr>\n    <tr>\n      <th>898842</th>\n      <td>32438591265832</td>\n      <td>96436391031185832</td>\n      <td>环保设备</td>\n      <td>4</td>\n      <td>60</td>\n      <td>{\"cargoName\":\"环保设备\",\"cargoText\":\"环保设备\",\"truckT...</td>\n    </tr>\n    <tr>\n      <th>898843</th>\n      <td>32438591885291</td>\n      <td>3988216899619015291</td>\n      <td>煤炭</td>\n      <td>6</td>\n      <td>73</td>\n      <td>{\"cargoName\":\"煤炭\",\"cargoText\":\"煤炭\",\"truckTypeN...</td>\n    </tr>\n    <tr>\n      <th>898844</th>\n      <td>32438592920699</td>\n      <td>96436391873310699</td>\n      <td>煤块</td>\n      <td>6</td>\n      <td>73</td>\n      <td>{\"cargoName\":\"煤块\",\"cargoText\":\"煤块\",\"truckTypeN...</td>\n    </tr>\n    <tr>\n      <th>898845</th>\n      <td>32438593022063</td>\n      <td>96436391207002063</td>\n      <td>玉米作物（干玉米）</td>\n      <td>10002</td>\n      <td>10024</td>\n      <td>{\"cargoName\":\"作物干玉米\",\"cargoText\":\"玉米作物（干玉米）\",\"...</td>\n    </tr>\n  </tbody>\n</table>\n<p>898846 rows × 6 columns</p>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(file_name,sep='\u0001')\n",
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "              cargo_id           shipper_id cargo_name  first_category_id  \\\n0       32413588863147  3988213644352553147         化肥                 13   \n1       32414135827834    96436391904597834         椰子              10000   \n2       32414137990792  4000000000002670792        胡萝卜              10001   \n3       32414138875050  4000000000008475050         色粉                 14   \n4       32414140065568  4000000015543125568       食品饮料                 10   \n...                ...                  ...        ...                ...   \n898841  32438590365953    95819431532065953        包装盒                  7   \n898842  32438591265832    96436391031185832       环保设备                  4   \n898843  32438591885291  3988216899619015291         煤炭                  6   \n898844  32438592920699    96436391873310699         煤块                  6   \n898845  32438593022063    96436391207002063  玉米作物（干玉米）              10002   \n\n        second_category_id                                          nlpresult  \\\n0                      156  {\"cargoName\":\"化肥\",\"cargoText\":\"化肥\",\"truckTypeN...   \n1                    10011  {\"cargoName\":\"椰子\",\"cargoText\":\"椰子\",\"truckTypeN...   \n2                    10014  {\"cargoName\":\"胡萝卜\",\"cargoText\":\"胡萝卜\",\"truckTyp...   \n3                      106  {\"cargoName\":\"色粉\",\"cargoText\":\"色粉\",\"truckTypeN...   \n4                      200  {\"cargoName\":\"食品饮料\",\"cargoText\":\"食品饮料\",\"truckT...   \n...                    ...                                                ...   \n898841                  77  {\"cargoName\":\"包装盒\",\"cargoText\":\"包装盒\",\"truckTyp...   \n898842                  60  {\"cargoName\":\"环保设备\",\"cargoText\":\"环保设备\",\"truckT...   \n898843                  73  {\"cargoName\":\"煤炭\",\"cargoText\":\"煤炭\",\"truckTypeN...   \n898844                  73  {\"cargoName\":\"煤块\",\"cargoText\":\"煤块\",\"truckTypeN...   \n898845               10024  {\"cargoName\":\"作物干玉米\",\"cargoText\":\"玉米作物（干玉米）\",\"...   \n\n         解析结果  \n0          化肥  \n1          椰子  \n2         胡萝卜  \n3          色粉  \n4        食品饮料  \n...       ...  \n898841    包装盒  \n898842   环保设备  \n898843     煤炭  \n898844     煤块  \n898845  作物干玉米  \n\n[898846 rows x 7 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>cargo_id</th>\n      <th>shipper_id</th>\n      <th>cargo_name</th>\n      <th>first_category_id</th>\n      <th>second_category_id</th>\n      <th>nlpresult</th>\n      <th>解析结果</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>32413588863147</td>\n      <td>3988213644352553147</td>\n      <td>化肥</td>\n      <td>13</td>\n      <td>156</td>\n      <td>{\"cargoName\":\"化肥\",\"cargoText\":\"化肥\",\"truckTypeN...</td>\n      <td>化肥</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>32414135827834</td>\n      <td>96436391904597834</td>\n      <td>椰子</td>\n      <td>10000</td>\n      <td>10011</td>\n      <td>{\"cargoName\":\"椰子\",\"cargoText\":\"椰子\",\"truckTypeN...</td>\n      <td>椰子</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>32414137990792</td>\n      <td>4000000000002670792</td>\n      <td>胡萝卜</td>\n      <td>10001</td>\n      <td>10014</td>\n      <td>{\"cargoName\":\"胡萝卜\",\"cargoText\":\"胡萝卜\",\"truckTyp...</td>\n      <td>胡萝卜</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>32414138875050</td>\n      <td>4000000000008475050</td>\n      <td>色粉</td>\n      <td>14</td>\n      <td>106</td>\n      <td>{\"cargoName\":\"色粉\",\"cargoText\":\"色粉\",\"truckTypeN...</td>\n      <td>色粉</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>32414140065568</td>\n      <td>4000000015543125568</td>\n      <td>食品饮料</td>\n      <td>10</td>\n      <td>200</td>\n      <td>{\"cargoName\":\"食品饮料\",\"cargoText\":\"食品饮料\",\"truckT...</td>\n      <td>食品饮料</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>898841</th>\n      <td>32438590365953</td>\n      <td>95819431532065953</td>\n      <td>包装盒</td>\n      <td>7</td>\n      <td>77</td>\n      <td>{\"cargoName\":\"包装盒\",\"cargoText\":\"包装盒\",\"truckTyp...</td>\n      <td>包装盒</td>\n    </tr>\n    <tr>\n      <th>898842</th>\n      <td>32438591265832</td>\n      <td>96436391031185832</td>\n      <td>环保设备</td>\n      <td>4</td>\n      <td>60</td>\n      <td>{\"cargoName\":\"环保设备\",\"cargoText\":\"环保设备\",\"truckT...</td>\n      <td>环保设备</td>\n    </tr>\n    <tr>\n      <th>898843</th>\n      <td>32438591885291</td>\n      <td>3988216899619015291</td>\n      <td>煤炭</td>\n      <td>6</td>\n      <td>73</td>\n      <td>{\"cargoName\":\"煤炭\",\"cargoText\":\"煤炭\",\"truckTypeN...</td>\n      <td>煤炭</td>\n    </tr>\n    <tr>\n      <th>898844</th>\n      <td>32438592920699</td>\n      <td>96436391873310699</td>\n      <td>煤块</td>\n      <td>6</td>\n      <td>73</td>\n      <td>{\"cargoName\":\"煤块\",\"cargoText\":\"煤块\",\"truckTypeN...</td>\n      <td>煤块</td>\n    </tr>\n    <tr>\n      <th>898845</th>\n      <td>32438593022063</td>\n      <td>96436391207002063</td>\n      <td>玉米作物（干玉米）</td>\n      <td>10002</td>\n      <td>10024</td>\n      <td>{\"cargoName\":\"作物干玉米\",\"cargoText\":\"玉米作物（干玉米）\",\"...</td>\n      <td>作物干玉米</td>\n    </tr>\n  </tbody>\n</table>\n<p>898846 rows × 7 columns</p>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "def parse_json(item):\n",
    "    json_obj = json.loads(item[\"nlpresult\"])\n",
    "    if json_obj and json_obj[\"cargoName\"]:\n",
    "        return json_obj[\"cargoName\"]\n",
    "    else:\n",
    "        return \"\"\n",
    "df[\"解析结果\"] = df.apply(parse_json,axis=1)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "              cargo_id           shipper_id cargo_name  first_category_id  \\\n0       32413588863147  3988213644352553147         化肥                 13   \n1       32414135827834    96436391904597834         椰子              10000   \n2       32414137990792  4000000000002670792        胡萝卜              10001   \n3       32414138875050  4000000000008475050         色粉                 14   \n4       32414140065568  4000000015543125568       食品饮料                 10   \n...                ...                  ...        ...                ...   \n898841  32438590365953    95819431532065953        包装盒                  7   \n898842  32438591265832    96436391031185832       环保设备                  4   \n898843  32438591885291  3988216899619015291         煤炭                  6   \n898844  32438592920699    96436391873310699         煤块                  6   \n898845  32438593022063    96436391207002063  玉米作物（干玉米）              10002   \n\n        second_category_id                                          nlpresult  \\\n0                      156  {\"cargoName\":\"化肥\",\"cargoText\":\"化肥\",\"truckTypeN...   \n1                    10011  {\"cargoName\":\"椰子\",\"cargoText\":\"椰子\",\"truckTypeN...   \n2                    10014  {\"cargoName\":\"胡萝卜\",\"cargoText\":\"胡萝卜\",\"truckTyp...   \n3                      106  {\"cargoName\":\"色粉\",\"cargoText\":\"色粉\",\"truckTypeN...   \n4                      200  {\"cargoName\":\"食品饮料\",\"cargoText\":\"食品饮料\",\"truckT...   \n...                    ...                                                ...   \n898841                  77  {\"cargoName\":\"包装盒\",\"cargoText\":\"包装盒\",\"truckTyp...   \n898842                  60  {\"cargoName\":\"环保设备\",\"cargoText\":\"环保设备\",\"truckT...   \n898843                  73  {\"cargoName\":\"煤炭\",\"cargoText\":\"煤炭\",\"truckTypeN...   \n898844                  73  {\"cargoName\":\"煤块\",\"cargoText\":\"煤块\",\"truckTypeN...   \n898845               10024  {\"cargoName\":\"作物干玉米\",\"cargoText\":\"玉米作物（干玉米）\",\"...   \n\n         解析结果       原始文本  \n0          化肥         化肥  \n1          椰子         椰子  \n2         胡萝卜        胡萝卜  \n3          色粉         色粉  \n4        食品饮料       食品饮料  \n...       ...        ...  \n898841    包装盒        包装盒  \n898842   环保设备       环保设备  \n898843     煤炭         煤炭  \n898844     煤块         煤块  \n898845  作物干玉米  玉米作物（干玉米）  \n\n[898846 rows x 8 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>cargo_id</th>\n      <th>shipper_id</th>\n      <th>cargo_name</th>\n      <th>first_category_id</th>\n      <th>second_category_id</th>\n      <th>nlpresult</th>\n      <th>解析结果</th>\n      <th>原始文本</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>32413588863147</td>\n      <td>3988213644352553147</td>\n      <td>化肥</td>\n      <td>13</td>\n      <td>156</td>\n      <td>{\"cargoName\":\"化肥\",\"cargoText\":\"化肥\",\"truckTypeN...</td>\n      <td>化肥</td>\n      <td>化肥</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>32414135827834</td>\n      <td>96436391904597834</td>\n      <td>椰子</td>\n      <td>10000</td>\n      <td>10011</td>\n      <td>{\"cargoName\":\"椰子\",\"cargoText\":\"椰子\",\"truckTypeN...</td>\n      <td>椰子</td>\n      <td>椰子</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>32414137990792</td>\n      <td>4000000000002670792</td>\n      <td>胡萝卜</td>\n      <td>10001</td>\n      <td>10014</td>\n      <td>{\"cargoName\":\"胡萝卜\",\"cargoText\":\"胡萝卜\",\"truckTyp...</td>\n      <td>胡萝卜</td>\n      <td>胡萝卜</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>32414138875050</td>\n      <td>4000000000008475050</td>\n      <td>色粉</td>\n      <td>14</td>\n      <td>106</td>\n      <td>{\"cargoName\":\"色粉\",\"cargoText\":\"色粉\",\"truckTypeN...</td>\n      <td>色粉</td>\n      <td>色粉</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>32414140065568</td>\n      <td>4000000015543125568</td>\n      <td>食品饮料</td>\n      <td>10</td>\n      <td>200</td>\n      <td>{\"cargoName\":\"食品饮料\",\"cargoText\":\"食品饮料\",\"truckT...</td>\n      <td>食品饮料</td>\n      <td>食品饮料</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>898841</th>\n      <td>32438590365953</td>\n      <td>95819431532065953</td>\n      <td>包装盒</td>\n      <td>7</td>\n      <td>77</td>\n      <td>{\"cargoName\":\"包装盒\",\"cargoText\":\"包装盒\",\"truckTyp...</td>\n      <td>包装盒</td>\n      <td>包装盒</td>\n    </tr>\n    <tr>\n      <th>898842</th>\n      <td>32438591265832</td>\n      <td>96436391031185832</td>\n      <td>环保设备</td>\n      <td>4</td>\n      <td>60</td>\n      <td>{\"cargoName\":\"环保设备\",\"cargoText\":\"环保设备\",\"truckT...</td>\n      <td>环保设备</td>\n      <td>环保设备</td>\n    </tr>\n    <tr>\n      <th>898843</th>\n      <td>32438591885291</td>\n      <td>3988216899619015291</td>\n      <td>煤炭</td>\n      <td>6</td>\n      <td>73</td>\n      <td>{\"cargoName\":\"煤炭\",\"cargoText\":\"煤炭\",\"truckTypeN...</td>\n      <td>煤炭</td>\n      <td>煤炭</td>\n    </tr>\n    <tr>\n      <th>898844</th>\n      <td>32438592920699</td>\n      <td>96436391873310699</td>\n      <td>煤块</td>\n      <td>6</td>\n      <td>73</td>\n      <td>{\"cargoName\":\"煤块\",\"cargoText\":\"煤块\",\"truckTypeN...</td>\n      <td>煤块</td>\n      <td>煤块</td>\n    </tr>\n    <tr>\n      <th>898845</th>\n      <td>32438593022063</td>\n      <td>96436391207002063</td>\n      <td>玉米作物（干玉米）</td>\n      <td>10002</td>\n      <td>10024</td>\n      <td>{\"cargoName\":\"作物干玉米\",\"cargoText\":\"玉米作物（干玉米）\",\"...</td>\n      <td>作物干玉米</td>\n      <td>玉米作物（干玉米）</td>\n    </tr>\n  </tbody>\n</table>\n<p>898846 rows × 8 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def parse_json1(item):\n",
    "    json_obj = json.loads(item[\"nlpresult\"])\n",
    "    if json_obj and \"cargoText\" in json_obj:\n",
    "        return json_obj[\"cargoText\"]\n",
    "    else:\n",
    "        return \"\"\n",
    "df[\"原始文本\"] = df.apply(parse_json1,axis=1)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "# def check_if_has_category_id(item):\n",
    "#     if item[\"second_category_id\"] == -1:\n",
    "#         return 0\n",
    "#     else:\n",
    "#         return 1\n",
    "# df[\"has_category_id\"] = df.apply(check_if_has_category_id,axis=1)\n",
    "# df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "# !pip install swifter\n",
    "# !pip install pandarallel\n",
    "# from pandarallel import pandarallel\n",
    "# pandarallel.initialize()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "KeyboardInterrupt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import swifter\n",
    "import requests\n",
    "def get_new_result(item):\n",
    "    cargo_text = item[\"原始文本\"]\n",
    "    url = \"http://beetle.amh-group.com/beetle/cargo_name/cargo_name_identify\"\n",
    "    post_body = {\n",
    "        \"cargoName\":cargo_text,\n",
    "        \"Transfer\":1\n",
    "    }\n",
    "    headers = {\n",
    "        'Content-Type':\"application/json\"\n",
    "    }\n",
    "\n",
    "    req = requests.post(url,data=json.dumps(post_body),headers=headers)\n",
    "    json_obj = req.json()['data'][\"cargoNameInfo\"]\n",
    "    return json_obj[\"cargoName\"],str(json_obj[\"type\"])\n",
    "\n",
    "df[[\"新接口结果\",'new_api_type']] = df.apply(get_new_result,axis=1,result_type=\"expand\")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from functools import partial\n",
    "import multiprocessing as mp\n",
    "#\n",
    "import swifter\n",
    "import requests\n",
    "def get_new_result(item):\n",
    "    cargo_text = item[\"原始文本\"]\n",
    "    url = \"http://beetle.amh-group.com/beetle/cargo_name/cargo_name_identify\"\n",
    "    post_body = {\n",
    "        \"cargoName\":cargo_text,\n",
    "        \"Transfer\":1\n",
    "    }\n",
    "    headers = {\n",
    "        'Content-Type':\"application/json\"\n",
    "    }\n",
    "\n",
    "    req = requests.post(url,data=json.dumps(post_body),headers=headers)\n",
    "    #json_obj = req.json()['data'][\"cargoNameInfo\"]\n",
    "    return req.json()\n",
    "\n",
    "def parallelize_df(df, func, num_processes=6):\n",
    "    df_split = np.array_split(df, num_processes)\n",
    "    pool =  mp.Pool(num_processes)\n",
    "    data = pd.concat(pool.map(func, df_split))\n",
    "    pool.close()\n",
    "    pool.join()\n",
    "    return data\n",
    "\n",
    "\n",
    "def parallelize_apply_func(func,df):\n",
    "    df[\"column_output\"] = df.apply(func, axis=1)\n",
    "    return df\n",
    "\n",
    "df[\"新接口结果\"] = parallelize_df(df,partial(parallelize_apply_func,get_new_result),8)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}